package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.entity.UserInfo;
import com.example.demo.exception.BusyException;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.mapper.UserInfoMapper;
import com.example.demo.mapper.WarningInfoMapper;
import com.example.demo.entity.Message;
import com.example.demo.entity.Student;
import com.example.demo.entity.WarningInfos;
import com.example.demo.util.EmailUtil;
import com.example.demo.util.WarningEmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger;

@Controller
public class AddWarningInfoController {
    @Autowired
    private WarningInfoMapper wm;
    @Autowired
    private StudentMapper sm;
    @Autowired
    private UserInfoMapper uim;
    @Resource(name = "sendEmail")
    private Executor emailExecutor;

@RequestMapping("/addwarninginfo")
    public void addWarningInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
    int stuid=Integer.valueOf(request.getParameter("stuid"));
    String stuname=request.getParameter("stuname");
    String stumajor=request.getParameter("stumajor");
    String stuclassi=request.getParameter("stuclassi");
    int warningid=Integer.valueOf(request.getParameter("warningid"));
    int warningtype=Integer.valueOf(request.getParameter("warningtype"));
    String term=request.getParameter("term");
    String detail=request.getParameter("detail");
    List<WarningInfos> list=new ArrayList<>();
    list=wm.selectAll();
    boolean isExistWarningid=list.stream().anyMatch(warning -> warning.getWarningid()==warningid);
      Student  s = sm.selectStuById(stuid);
    if (s != null) {
        if (s.getName().equals(stuname)&&s.getMajor().equals(stumajor)&&s.getClassi().equals(stuclassi)) {
            if (detail != null && !"".equals(detail)) {
                if (!isExistWarningid) {
                    Date date = new Date(System.currentTimeMillis());
                    Date date1 = new Date(System.currentTimeMillis());
                    WarningEmailUtil warn=new WarningEmailUtil();
                    UserInfo userInfo = uim.selectByName(stuname);

                    EmailUtil.sendEmailAsyncWithRetry(() -> warn.sendEmail("3121289785@qq.com",
                            Objects.isNull(userInfo) ? "3121289785@qq.com" : userInfo.getEmail(),detail), 3, emailExecutor);

                    WarningInfos w = new WarningInfos(warningid, stuname, stuid, stumajor, stuclassi, warningtype, detail, term,date, date1,1,1);
                    int n = wm.addWarningInfo(w);
                    if (n > 0) { response.getWriter().write(JSONObject.toJSONString(Message.success("Warning info send success")));
                    } else response.getWriter().write(JSONObject.toJSONString(Message.failure("Warninginfo send failure")));
                } else response.getWriter().write(JSONObject.toJSONString(Message.failure("Warningid is exist and Please change")));
            } else response.getWriter().write(JSONObject.toJSONString(Message.failure("Your input student information is error")));
        }else response.getWriter().write(JSONObject.toJSONString(Message.failure("Your input date is illegal")));
    }else response.getWriter().write(JSONObject.toJSONString(Message.failure("Can not find this student information")));
    }
}
